// ==UserScript==
// @name         ishumei-小助手
// @namespace             https://gitee.com/monkeyshow/tm_scripts
// @supportURL            https://gitee.com/monkeyshow/tm_scripts
// @updateURL             https://gitee.com/monkeyshow/tm_scripts/raw/master/ishumei/ishumei-helper.js
// @downloadURL           https://gitee.com/monkeyshow/tm_scripts/raw/master/ishumei/ishumei-helper.js
// @version      0.2.0
// @description  ishumei-小助手
// @author       jianjian.huang
// @match        *://op.ishumei.com/*
// @grant        unsafeWindow
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_xmlhttpRequest
// @run-at       document-idle
// @require      https://cdn.staticfile.org/jquery/3.5.0/jquery.min.js
// ==/UserScript==
(() => {
  'use strict'
  const KEY_BOARD_CODE = { ENTER: 13, SPACE: 32, MINI_PLUS: 43, MINI_MINUS: 45, ZERO: 48, P: 80, R: 82, S: 83, W: 87 };
  const MouseClickEvent = document.createEvent("MouseEvents");
  const ElementEvent = document.createEvent("UIEvents");
  MouseClickEvent.initEvent("click", true, true);
  ElementEvent.initEvent("keydown", true, true);

  function getValue(name) {
    return GM_getValue(name);
  }

  function setValue(name, value) {
    GM_setValue(name, value);
  }

  function clog() {
    console.group('[ishumei-小助手]')
    let args = Array.prototype.slice.call(arguments);
    args.forEach(element => {
      console.log(element);
    });
    console.groupEnd()
  }

  function IshumeiPlugin() {
    this.init = () => {
      this.addGMStyle()
      this.main()
    }

    this.main = () => {
      let $container = $('<div class="lh-container" id="dm-notif"></div>');
      // 按钮
      let $btn_trim_space = $('<button type="button" class="lh-btn">光标在文本框中按下空格键，即可去除空格!</button>');
      // 按钮外部的div
      let $btn_div = $("<div/>").append($btn_trim_space);
      $container.append($btn_div);
      // 添加到页面中
      $("#root").append($container);

      // 按钮事件绑定
      $btn_trim_space.click(() => {
        clog('去除空格！')
      });
      // 绑定快捷键
      $(document).keypress((e) => {
        let keyCode = e.keyCode || e.which || e.charCode;
        let altKey = e.altKey;
        let ctrlKey = e.ctrlKey;
        let shiftKey = e.shiftKey;
        clog(`document keypress: keycode=${e.keyCode},alt=${altKey},ctrl=${ctrlKey},shift=${shiftKey}`);
        // 整个文档接收到了快捷键操作
        if (keyCode == KEY_BOARD_CODE.SPACE && e.target.nodeName=='TEXTAREA' && e.target.className=='ant-input') {
          this.trimCurrent();
        }
      });
    };
    this.trimCurrent = () => {
      let $textarea = $('textarea.ant-input:focus');
      let oldStr = $textarea.parent().prev().text();
      let repStr = oldStr.replace(/\s+/g, "");
      clog(`old=${oldStr}-new=${repStr}`);
      $textarea.val(repStr);
      $textarea.get(0).dispatchEvent(ElementEvent);
    };
    this.addGMStyle = () => {
      GM_addStyle(`
      .lh-btn {color: #fff;background-color: #007bff;border-color: #007bff;padding: .375rem .75rem;border-radius: .25rem;}
      .lh-container {position: fixed;bottom: 5px;right: 5px;margin-left: 5px;z-index: 3000;width:226px;background-color:pink;overflow:hidden;}
      `)
    };
  }

  if ($("#root").length) {
    clog('hello ishumei!')
    let plugin = new IshumeiPlugin();
    plugin.init();
  }
})();